package com.singing.manager;

import com.singing.dao.SingScoreDao;
import com.singing.dao.SingerDao;
import com.singing.model.Command;
import com.singing.model.SingScore;
import com.singing.model.Singer;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class AllSingerScoreManager extends BaseManager {


    private static final int RELOAD = 1;
    SingScoreDao singScoreDao = new SingScoreDao();
    SingerDao singerDao = new SingerDao();

    public AllSingerScoreManager(String title, List<Command> commandList) {
        super(title, commandList);
    }


    @Override
    protected int processCommand(int command) {

        switch (command) {
            case RELOAD:
                break;
        }
        return 0;
    }

    @Override
    protected void printTitleExtra() {
        showSingerScore();
    }


    private void showSingerScore() {

        List<Singer> singers = singerDao.selectAll();
        //展示歌手评分
        System.out.println("歌手id\t\t歌手名称\t\t平均分（去除最高分和最低分）\t\t总分（去除最高分和最低分）");
        System.out.println();
        for (Singer singer : singers) {

            if (singer == null) {
                System.err.println("没有该歌手");
                return;
            }

            List<SingScore> singScores = singScoreDao.selectListBySingerId(singer.getId());
            // 判断是否为空
            if (singScores == null || singScores.size() <= 0) {
                System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t没有评分" + "\t\t没有评分");
                continue;
            }


            // 计算平均分，使用stream

            List<SingScore> sorted = singScores.stream().sorted(Comparator.comparingInt(SingScore::getScore)).collect(Collectors.toList());
            // 去掉最高分和最低分
            sorted.remove(0);

            sorted.remove(sorted.size() - 1);

            // 计算平均分,使用stream
            List<Integer> scores = sorted.stream().map(SingScore::getScore).collect(Collectors.toList());
            // 求和
            int sum = scores.stream().reduce(0, Integer::sum);

            double avgScore = (double) sum / scores.size();
            System.out.println(singer.getId() + "\t\t" + singer.getName() + "\t\t" + avgScore + "\t\t" + sum);
            System.out.println();
        }

    }


}
